<template>
  <MaskContainer ref="maskRemark" :main-style="maskContainerStyle" :has-btn="false" :has-close="true" title="备注列表" >
    <div class="remark-container">
      <div v-loading="listLoading" class="remark-table-container">
        <el-table
          v-if="remarkList.data"
          :data="remarkList.data.items || []"
          :border="true"
          max-height="390"
          header-row-class-name="remark-tabel-header"
          header-cell-class-name="remark-tabel-header-cell"
          cell-class-name="remark-tabel-cell"
          style="width: 100%;border-radius: 8px">
          <el-table-column align="center" label="显示" width="64">
            <template slot-scope="scope">
              <el-checkbox v-model="scope.row.isSelect" class="radio-style" @change="changeIsSelect(scope.row)"/>
            </template>
          </el-table-column>
          <el-table-column align="center" label="日期" width="150">
            <template slot-scope="scope">
              <div class="tabel-content">{{ scope.row.time | standardFormatNoHour }}</div>
            </template>
          </el-table-column>
          <el-table-column align="left" label="备注信息" width="174">
            <template slot-scope="scope">
              <!-- <el-scrollbar class="scroll-container"> -->
              <div v-if="!scope.row._isEdit" class="tabel-content">
                <el-tooltip v-if="scope.row.content && scope.row.content.length > 50" placement="top" effect="dark">
                  <div slot="content" style="max-width: 300px;">{{ scope.row.content }}</div>
                  <span class="content">{{ scope.row.content.substr(0, 50) }}...</span>
                </el-tooltip>
                <span v-else>{{ scope.row.content || '--' }}</span>
              </div>
              <div v-if="scope.row._isEdit" class="tabel-input">
                <!-- <input v-model="scope.row.editContent" maxlen gth="500" type="text" > -->
                <textarea v-model="scope.row.editContent" maxlength="500" rows="4"/>
              </div>
              <!-- </el-scrollbar> -->
            </template>
          </el-table-column>
          <el-table-column align="center" label="人员" width="91">
            <template slot-scope="scope">
              <div class="tabel-content">{{ scope.row.member }}</div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="91">
            <template slot-scope="scope">
              <div v-loading="scope.row._loading" class="flex-center">
                <div v-if="!scope.row._isEdit" class="button-text" @click="toEditTab(scope.row, true)">编辑</div>
                <div v-if="scope.row._isEdit" class="button-text" @click="saveEdit(scope.row)">保存</div>
                <div v-if="scope.row._isEdit" class="button-text bl" @click="toEditTab(scope.row, false, scope.$index)">取消</div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="footer-container">
        <!-- <div style="flex:1;"/> -->
        <div class="add-btn" @click="addRemarks">添加 <i class="el-icon-plus" /></div>
      </div>
    </div>
  </MaskContainer>
</template>
<script>
import { mapState } from 'vuex';
import MaskContainer from './MaskContainer';
// API
import { getRemarkList, editRemarkApi } from '@/api/medium';
// filter
import { standardFormatNoHour } from '@/filters/date';
import ElMessage from '@/utils/message';

import setting from '@/assets/setting.png';
export default {
  components: { MaskContainer },
  filters: { standardFormatNoHour },
  props: {},
  data() {
    return {
      setting,
      maskContainerStyle: {
        width: '640px',
        height: '540px',
        borderRadius: '12px',
        padding: '24px'
      },
      page: 1,
      perPage: 20,
      remarkList: {},
      listLoading: false
    };
  },
  computed: {
    ...mapState({
      'userInfo': state => state.user.userInfo
    })
  },
  created() {
  },
  methods: {
    tabMaskShow(item) {
      const mediumId = item.mediumId;
      this.mediumId = mediumId;
      this.getRemarkData(mediumId);
      this.$refs.maskRemark.tabMaskShow();
    },
    async getRemarkData(mediumId) {
      const params = {
        id: mediumId || this.mediumId,
        page: this.page,
        perPage: this.perPage
      };
      this.listLoading = true;
      const result = await getRemarkList(params);
      this.listLoading = false;
      if (result.code === 200) {
        result.data.items.forEach(item => {
          item._isEdit = false;
        });
        this.remarkList = result;
      }
    },
    async changeIsSelect(item) {
      if (item.remarkId) {
        this.remarkList.data.items.forEach(i => {
          i.isSelect = false;
        });
        item.isSelect = true;
        item.mediumId = this.mediumId;
        const result = await editRemarkApi(item);
        if (result.code === 200) {
          this.getRemarkData();
          this.$emit('mutedUpdateMediumList', item.mediumId);
        } else {
          this.$message({
            type: 'error',
            message: result.message
          });
        }
      }
    },
    async saveEdit(item) {
      if (item.editContent === '') {
        this.$message({
          type: 'error',
          message: '请输入内容'
        });
      } else {
        const params = _.cloneDeep(item);
        params.content = params.editContent;
        params.mediumId = this.mediumId;
        delete params.editContent;
        delete params._isEdit;
        this.$set(item, '_loading', true);
        const result = await editRemarkApi(params);
        this.$set(item, '_loading', false);
        if (result.code === 200) {
          this.getRemarkData(params.mediumId);
          this.$emit('mutedUpdateMediumList', params.mediumId);
          ElMessage.success('保存成功');
        } else {
          this.$set(item, '_loading', false);
          this.$message({
            type: 'error',
            message: result.message
          });
        }
      }
    },
    toEditTab(item, type, index) {
      if (type) {
        item.editContent = item.content;
      } else if (!item.remarkId) {
        this.remarkList.data.items.splice(index, 1);
      }
      item._isEdit = type;
    },
    addRemarks(index) {
      const remarkList = this.remarkList.data.items;
      const hasAddItem = remarkList.some(i => {
        return i._isEdit;
      });
      if (hasAddItem) return;
      const nowtime = Math.round(new Date().getTime() / 1000);
      const addItem = {
        _isEdit: true,
        time: nowtime,
        content: '',
        editContent: '',
        isSelect: false,
        member: this.userInfo.userName
      };
      remarkList.unshift(addItem);
    }
  }
};
</script>
<style scoped lang="scss">
$MediumColor: #fd3264;
.remark-container {
  overflow: hidden;
  padding: 24px 0 0;
  height: 100%;
  position: relative;
  display: flex;
  flex-direction: column;
  .remark-table-container {
    flex: 1;
    .tabel-content {
      font-size: 14px;
    }
    .tabel-input {
      display: flex;
      input {
        width: 100%;
        height: 100%;
        font-size: 14px;
        text-indent: 6px;
        border:  1px solid #ddd;
        outline: none;
        border-radius: 24px;
      }
      textarea {
        width: 100%;
        height: 100%;
        border:  1px solid #ddd;
        outline: none;
        resize: none;
        font-size: 14px;
        text-indent: 6px;
        line-height: 1.3;
        border-radius: 4px;
      }
    }
    .tabel-btn-container {
      display: flex;
      align-items: center;
      justify-content: center;
      .tabel-btn {
        color: #406eff;
        font-size: 14px;
        cursor: pointer;
        &.mr-10 {
          margin-right: 10px;
        }
      }
    }
  }
  .footer-container {
    text-align: center;
    .add-btn {
      cursor: pointer;
      width: 120px;
      height: 32px;
      background-color: #406eff;
      border-radius: 4px;
      color: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 14px;
      font-weight: 500;
      margin: 27px auto 0;
      .el-icon-plus {
        font-size: 16px;
        margin-left: 4px;
      }
      &:hover {
        background-color: #1f4cdf;
      }
    }
  }
}
.radio-style {
  &::v-deep {
    .el-checkbox__input {
      border-radius: 100%;
      position: relative;
      .el-checkbox__inner {
        border-radius: 100%;
      }
      &.is-checked {
        .el-checkbox__inner::after {
          display: none;
        }
        .el-checkbox__inner::before {
          width: 4px;
          height: 4px;
          display: block;
          border-radius: 100%;
          background-color: #fff;
          content: "";
          position: absolute;
          left: 50%;
          top: 50%;
          transform: translate(-50%, -50%);
          -webkit-transition: -webkit-transform .15s ease-in;
          transition: -webkit-transform .15s ease-in;
          transition: transform .15s ease-in;
          transition: transform .15s ease-in, -webkit-transform .15s ease-in;
        }
      }
    }
  }
}
</style>
<style lang="scss">
$MediumColor: hsl(345, 98%, 59%);
.remark-container {
  .remark-tabel-header {
    .remark-tabel-header-cell {
      background-color: #F5F5FB !important;
      .cell {
        text-align: center;
      }
    }
  }
  .remark-table-cell {
    border-radius: 8px;
  }
}
</style>
